package 代码随想录.回溯.全排列;

import java.util.ArrayList;
import java.util.List;

public class LeetCode46 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) return res;
        backTracking(nums, path);
        return res;
    }

    private void backTracking(int[] nums, List<Integer> path) {
        if (path.size() == nums.length) {
            res.add(new ArrayList<>(path));
        }
        for (int i = 0; i < nums.length; i++) {
            if (path.contains(nums[i])) {
                continue;
            }
            path.add(nums[i]);
            backTracking(nums, path);
            path.remove(path.size() - 1);//回溯
        }
    }

    public static void main(String[] args) {
        LeetCode46 code = new LeetCode46();
        int[] nums = {1, 2, 3};
        System.out.println(code.permute(nums));
    }

}
